package org.hamster.multidropbox.dropbox.model
{
	import mx.collections.ArrayCollection;
	import mx.events.CollectionEvent;
	
	import org.hamster.dropbox.DropboxClient;
	import org.hamster.dropbox.DropboxConfig;
	import org.hamster.multidropbox.dropbox.constants.AppDropboxClientStatus;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.main.utils.PureMVCHelper;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class DropboxClientListProxy extends Proxy
	{
		public static const NAME:String = "org.hamster.multidropbox.dropbox.model.DropboxClientListProxy";
		
		public const dropboxClientList:ArrayCollection = new ArrayCollection();
		
		public function DropboxClientListProxy(data:Object=null)
		{
			super(NAME, data);
			dropboxClientList.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangeHandler);
		}
		
		private function collectionChangeHandler(event:CollectionEvent):void
		{
			this.sendNotification(AppDropboxEvent.DROPBOX_CLIENT_CHANGE, dropboxClientList);
			
			for each (var dropboxClient:AppDropboxClient in dropboxClientList) {
				if (dropboxClient.status == AppDropboxClientStatus.UNKNOWN) {
					dropboxClient.status = AppDropboxClientStatus.LOADING;
					PureMVCHelper.dropboxClientProxy.accountInfo(dropboxClient);
				}
			}
		}
		
		public function genClient():AppDropboxClient
		{
			var dropboxConfig:DropboxConfig = new DropboxConfig("wnl0erseogu50mk", "eypos93aukcysdw");
			//var dropboxConfig:DropboxConfig = new DropboxConfig('sjz3xsqdyd7yzuj', 'e88jvvaaenz8saz' );
			var dropboxClient:AppDropboxClient = new AppDropboxClient(dropboxConfig);
			return dropboxClient;
		}
		
		public function genAddClient():AppDropboxClient
		{
			var result:AppDropboxClient = genClient();
			dropboxClientList.addItem(result);
			return result;
		}
		
		public function getClientByUID(uid:int):AppDropboxClient
		{
			for each (var dropboxClient:AppDropboxClient in this.dropboxClientList) {
				if (dropboxClient.accountDetail.uid == uid) {
					return dropboxClient;
				}
			}
			return null;
		}
	}
}